<template>
  <div class="login" v-if="login">
    <div class="register-box setpassword" v-if="resetPassword">
      <div class="title">重置密码</div>
      <ul class="centen">
        <li><div class="input-box" :class="{'err':phones}">
          <input type="tel" maxlength="11" autocomplete="off" v-model="phone" id="phone" placeholder="请输入手机号"/>
        </div></li>
        <li><div class="input-box" :class="{'err':passwords}">
          <input :type="type" maxlength="20"  v-model="password" id="password" placeholder="请输入密码"/>
          </div>
          <div @click="get_see()" class="see-btn">
            <span  v-if="password_img" ><img src="../assets/notsee-icon.png" ></span>
            <span  v-else ><img src="../assets/see-icon.png" ></span>
          </div>
        </li>
        <li class="mgt59"><div class="input-box" :class="{'err':yzms}"><input type="tel" maxlength="4" autocomplete="off"  v-model="yzm" id="yzm" placeholder="请输入验证码"/></div><button  :disabled="disabled" @click="sendcode()" class="yzm-box">{{btntxt}}</button></li>
      </ul>
      <div @click="resetingPassword()" class="register_btn">确定</div>
      <div class="switch">想起密码，现在 <p @click="switch_box(0)">登录</p></div>
      <div class="close" @click="close()"></div>
    </div>
    <template v-else>
      <div class="register-box" v-if="register">
        <div class="title">手机注册</div>
        <ul class="centen">
          <li><div class="input-box" :class="{'err':names}">
            <input type="text" maxlength="15" autoComplete="off" id="name" v-model="name" placeholder="请输入昵称"/>
          </div></li>
          <li><div class="input-box" :class="{'err':phones}">
            <input type="tel" maxlength="11" autocomplete="off" v-model="phone" id="phone" placeholder="请输入手机号"/>
          </div></li>
          <li><div class="input-box" :class="{'err':passwords}">
            <input :type="type" maxlength="20"  v-model="password" id="password" placeholder="请输入密码"/>
            </div>
            <div @click="get_see()" class="see-btn">
              <span  v-if="password_img" ><img src="../assets/notsee-icon.png" ></span>
              <span  v-else ><img src="../assets/see-icon.png" ></span>
            </div>
          </li>
          <li><div class="input-box" :class="{'err':yzms}"><input type="tel" maxlength="4" autocomplete="off"  v-model="yzm" id="yzm" placeholder="请输入验证码"/></div><button  :disabled="disabled" @click="sendcode()" class="yzm-box">{{btntxt}}</button></li>
          <li><div class="input-box" :class="{'err':tjms}"><input type="text" maxlength="30" autocomplete="off"  v-model="tjm" id="tjm" placeholder="输入推荐码"/></div></li>
        </ul>
        <div class="clause-checked" @click="checked()">
          <div class="checked"><p v-if="checkedClause"></p></div>
          我已阅读并同意<router-link target="_blank" tag="a" :to="{path:'/clause'}" >《相关条款》</router-link>和
          <router-link target="_blank" tag="a" :to="{path:'/privacy'}" >《隐私政策》</router-link>
        </div>
        <div @click="get_register()" class="register_btn">注册</div>
        <div class="switch">已有账号，现在 <p @click="switch_box(0)">登录</p></div>
        <div class="close" @click="close()"></div>
      </div>
      <div class="login_box" v-else>
        <div class="nav_login"><p @click="get_nav(0)" :class="{'activ':index==0}">密码登录</p><p  @click="get_nav(1)" :class="{'activ':index==1}">验证码登录</p></div>
        <div v-if="index==0">
          <ul  class="centen">
            <li>
              <div class="input-box" :class="{'err':phones}">
                <input type="tel" maxlength="11" autocomplete="off" v-model="phone" id="phone" placeholder="输入ID或手机号"/>
              </div>
            </li>
            <li><div class="input-box" :class="{'err':passwords}"><input :type="type" maxlength="20" autocomplete="off" v-model="password" id="password" placeholder="请输入密码"/></div>
              <div @click="get_see()" class="see-btn">
                <span  v-if="password_img" ><img src="../assets/notsee-icon.png" ></span>
                <span  v-else ><img src="../assets/see-icon.png" ></span>
              </div>
            </li>
          </ul>
          <p class="not_password"><span @click="resetPassword = true">忘记密码？</span></p>
        </div>
        <div v-else>
          <ul  class="centen">
            <li><div class="input-box" :class="{'err':phones}"><input type="tel" maxlength="11" autocomplete="off" v-model="phone" id="phone" placeholder="输入手机号"/></div></li>
            <li><div class="input-box" :class="{'err':yzms}"><input type="tel" maxlength="4" autocomplete="off" v-model="yzm" id="yzm" placeholder="请输入验证码"/></div><button  :disabled="disabled" @click="sendcode()" class="yzm-box">{{btntxt}}</button></li>
          </ul>
        </div>
        <div class="login_btn_box">
          <div class="register_btn" @click="get_login()">登录</div>
          <div class="switch">未有账号，现在 <p @click="switch_box(1)">注册</p></div>
        </div>
        <div class="close" @click="close()"></div>
      </div>
    </template>
  </div>
</template>

<script>
import Bus from '../../config/bus.js'
export default {
 data () {
    return {
      resetPassword: false,
      login:false,
      type:'password',
      password_img:true,
      checkedClause:true,
      register:false,
      index:0,
      names:false,
      phones:false,
      passwords:false,
      yzms:false,
      tjms:false,
      name:'',
      phone:'',
      password:'',
      yzm:'',
      tjm:'',
      btn_key:true,
      disabled:false,
      time:60,
      btntxt:"发送验证码",
      djstime:null,
    }
  },
  watch: {
    login(newVal) {
      console.log(newVal)
      if(newVal) {
        window.addEventListener('keydown', this.bindLogin)
      } else {
        window.removeEventListener('keydown', this.bindLogin)
      }
    }
  },
  methods:{
    resetingPassword() {
      this.$api.post('/api/user/update/password', {
        code: this.yzm,
        mobile: this.phone,
        passWord: this.password
      }, res => {
        if (res.code === 200) {
          this.$confirm({
            type: '提示',
            time: '2',
            msg: '密码已重置，请使用新密码登录！'
          })
          this.resetPassword = false
          this.password = ''
          this.yzm= ''
        } else {
          this.$confirm({
            type: '提示',
            time: '1',
            msg: res.msg
          })
        }
      })
    },
    bindLogin(e) {
      if (e.keyCode == 13) {
        if (this.register) {
          this.get_register()
        } else {
          this.get_login()
        }
      }
    },
    sendcode(){
      this.timer();
    },
     //发送手机验证码倒计时
    timer() {
       if (this.time > 0) {
        this.disabled=true;
        this.time--;
        this.btntxt="倒计时"+this.time+"秒";
        this.djstime = setTimeout(this.timer, 1000);
       } else{
        this.time=0;
        this.btntxt="发送验证码";
        this.disabled=false;
       }
    },
    close(){
      var k = window.localStorage.getItem('logink');
      if(k==1){
        this.$router.go(-1);
        window.localStorage.removeItem('logink');
      }
      this.login=false;
      this.name='';
      this.phone='';
      this.password='';
      this.yzm='';
      this.tjm='';
      this.disabled = false;
      this.resetPassword = false
      this.time = 60;
      clearTimeout(this.djstime);
      this.btntxt="发送验证码";
    },
    get_see(){
      var k = this.password_img;
      console.log(k)
      if(k){
        this.password_img = false;
        this.type = 'text';
      }else{
        this.password_img = true;
        this.type = 'password';
      }
    },
    checked(){
      this.checkedClause?this.checkedClause=false:this.checkedClause=true;
    },
    get_nav(e){
      this.index = e;
      this.password='';
      this.yzm='';
    },
    switch_box(e){
      this.resetPassword = false
      this.disabled = false;
      this.time = 60;
      clearTimeout(this.djstime);
      this.btntxt="发送验证码";
      if(e==0){
        this.register = false;

      }else{
        this.register = true;
      }
      this.name='';
      this.phone='';
      this.password='';
      this.yzm='';
      this.tjm='';
    },
    get_login(){
      // if(!(/^1[3456789]\d{9}$/.test(this.phone))){
      //            this.phones = true;

      //           return false;
      //   }else{this.phones = false};
        if (this.index==0) {
          if(this.password==''){this.passwords = true;return}else{this.passwords = false}
        } else{
          if(this.yzm==''){this.yzms = true;return}else{this.yzms = false}
        }
      if (this.btn_key) {
        this.$api.post('/api/user/login',
        {
           code:this.yzm,
           mobile:this.phone,
           passWord:this.password
        }
        , r => {
                 this.btn_key = true;
                 if(r.code==500){
                    this.$confirm({
                        type:'提示',
                        time:'1',
                        msg:r.msg,
                    })
                 }
                 if(r.code==404){
                   this.$confirm({
                       type:'提示',
                       time:'1',
                       msg:r.msg,
                   })
                 }else{
                     var token = r.data.token;
                     var id = r.data.id
                     window.localStorage.setItem('token',token);
                     window.localStorage.setItem('id',id);
                     // this.$slots
                     if(r.code==200){
                       this.login = false;
                       this.phone='';
                       this.password='';
                       this.yzm='';
                       Bus.$emit('get_WebSocket', event.target);
                       this.login = false;
                     }
                    this.$confirm({
                        type:'提示',
                        time:'1',
                        msg:'登录成功！',
                    })
                 }
               })
      }
    },
    get_out(e){
       // let routes = this.$router.resolve({ name: e});
       // window.open(routes.href, "_blank");
       let {href}= this.$router.resolve({
          path: '/Clause',   // 这里写的是要跳转的路由地址
       });
       window.open(href, '_blank');
    },
    get_register() {
      if(this.name == '') {
        this.$confirm({
          type: '提示',
          time: 1,
          msg: '请输入昵称'
        })
        this.names = true;
        return
      } else {
        this.names = false
      }
      if(!(/^1[3456789]\d{9}$/.test(this.phone))) {
                this.phones = true;
                this.$confirm({
                    type:'提示',
                    time:'1',
                    msg:'手机号码错误',
                })
                return false;
        }else{this.phones = false}
      if(this.password == '') {
        this.$confirm({
          type: '提示',
          time: '1',
          msg: '请输入密码'
        })
        this.passwords = true;
        return 
      } else {
        this.passwords = false
      }
      if(this.yzm == '') {
        this.$confirm({
          type: "提示",
          time: '1',
          msg: '请输入验证码'
        })
        this.yzms = true;return
      } else {
        this.yzms = false
      }
      if(this.checkedClause == false) {
        return
      }
      if(this.btn_key){
        this.btn_key = false
        this.$api.post('/api/user/register',
            {
              code:this.yzm,
              invitationCode:this.tjm,
              mobile:this.phone,
              nickName:this.name,
              passWord:this.password
            }
              , r => {
                this.btn_key = true;
                console.log(r.msg)
                if(r.code==200){
                  this.login = false;
                  this.name='';
                  this.phone='';
                  this.password='';
                  this.yzm='';
                  this.tjm='';
                  this.$confirm({
                      type:'提示',
                      time:'1',
                      msg:'注册成功！请登录',
                  });
                  clearTimeout(this.djstime);
                  this.btntxt="发送验证码";
                };
        })
      }
    }
  },
  created() {
      Bus.$on('get_register', target => {
          this.login = true;
          this.register = true;
      });
      Bus.$on('get_login', target => {
          this.login = true;
          this.register = false;
      });
      // addEventListener('')
      console.log(333)
  }
  // mounted(){
  //     document.getElementsByTagName("body")[0].className="fixed";
  // },
  // beforeDestroy(){
  //     document.body.removeAttribute("class","fixed");
  // },
}
</script>

<style scoped>
  .close{
    background-image: url(../assets/close-icon1.png);
    width: 40px;
    height: 40px;
    background-size: 100% 100%;
    cursor: pointer;
    position: absolute;
    top: 0;
    right: 0;
  }
  .fixed{
    position: fixed;
  }
    a{
      text-decoration: none;
      color: #999999;
    }
    /* .err{
      border: 1px solid red !important;
    } */
    .login_btn_box{
      position: absolute;
      left: 0;
      right: 0;
      top:412px ;
    }
    .not_password{
      margin-top: 14px;
      display: flex;
      justify-content: flex-end;
      color: #fff;
      font-size: 16px;
    }
    .not_password span{
      cursor: pointer;
    }
    .login_box{
      width: 583px;
      height: 570px;
      padding: 45px 42px;
      background-color: #333842;
      position: relative;
    }
    .nav_login{
      display: flex;
      justify-content: center;
      margin-bottom: 40px;
    }
    .nav_login>p{
      padding: 5px 15px;
      color: #FFFFFF;
      font-size: 22px;
      margin: 0 20px;
      border-bottom: 2px solid #333842;
      cursor: pointer;
    }
    .nav_login .activ{
      color: #FFA22A;
      border-bottom: 2px solid #FFA22A;
    }
    .switch{
      display: flex;
      justify-content: center;
      align-items: center;
      color: #999999;
      margin: 24px auto;
    }
    .switch p{
      color: #FFA22A;
      margin-left: 5px;
      cursor: pointer;
    }
    .login{
      position: fixed;
      top:0;
      left: 0;
      right: 0;
      bottom: 0;
      min-height: 900px;
      min-width: 1200px;
      background-color: rgba(0,0,0,.5);
      z-index: 999;
      display: flex;
      justify-content: center;
      align-items: center;

    }
    .register-box{
      width: 582px;
      height: 692px;
      background-color: #333842;
      padding: 45px 42px;
      box-sizing: border-box;
      position: relative;
    }
    .register-box .title{
      font-size: 22px;
      color: #FFFFFF;
      text-align: left;
    }
    .register-box .centen{
      margin-top: 50px;
    }
     .centen>li{
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: #24282F;
      margin-bottom: 16px;
      position: relative;
    }
    .input-box{
      width: 100%;
      height: 53px;
      line-height: 53px;
      border: 1px solid #24282F;
    }
    .input-box input{
      width: 100%;
      height:100%;
      border: none;
      background-color: #24282F;
      padding-left: 15px;
      box-sizing: border-box;
      font-size: 16px;
      outline: none;
      color: #FFFFFF;
    }
    .see-btn{
      position: absolute;
      top: 50%;
      right: 1px;
      padding-right: 17px;
      width: 47px;
      height: 20px;
      margin-top: -10px;
      cursor: pointer;
      background-color:#24282F ;
    }
    .see-btn img{
      width: 30px;
      height: 100%;
    }
    .yzm-box{
      position: absolute;
      top: 50%;
      right: 17px;
      font-size: 16px;
      color: #FFFFFF;
      height: 30px;
      line-height: 30px;
      margin-top: -15px;
      cursor: pointer;
      border: none;
      background-color: #24282F;
    }
    .clause-checked{
      margin: 36px auto;
      display: flex;
      justify-content: center;
      align-items: center;
      color: #999999;
      cursor: pointer;
      width: 360px;
    }
    .clause-checked a:hover{
      color: #FFFFFF;
    }
    .checked{
      width: 15px;
      height: 15px;
      border-radius: 50%;
      border: 1px solid #999999;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-right: 5px;
    }
    .checked p{
      width: 9px;
      height: 9px;
      background-color: #FFA22A;
      border-radius: 50%;
    }
    .register_btn{
      width: 402px;
      height: 56px;
      line-height: 56px;
      background-color: #FFA22A;
      border-radius: 5px;
      font-weight: bold;
      color: #000000;
      font-size: 20px;
      margin:  0 auto;
      cursor: pointer;
    }
    .setpassword{
      height: 570px;
    }
    .mgt59{
      margin-bottom: 59px !important;
    }
</style>
